const app = getApp();

Page({
    data: {
        questionList: [],
        currentIndex: 0,
        selectedAnswer: [],
        isAnimate: false,
        userInfo: {},
        questionRightAnswer: [],
        hostId: '',
        notSameArrIndex: []
    },

    onLoad: function (options) {
        this.setData({
            userInfo: JSON.parse(options.userInfo),
            hostId: options.hostId
        })
        // 获取题目列表
        this.getQuestionList().then(res => {
            res.data.forEach(item => {
                this.data.questionRightAnswer.push(item.right);
            })
            this.setData({
                questionList: res.data
            })
        })
    },
    getQuestionList() {
        return new Promise((resolve, reject) => {
            app.cloud({
                name: 'login',
                data: {
                    $url: 'get_question_list'
                }
            }).then(res => {
                resolve(res);
            }).catch(err => {
                reject(err);
            })
        })
    },
    // 选择答案
    chooseAnswer(evt) {
        let { answer, index } = evt.currentTarget.dataset;
        this.data.selectedAnswer.push(answer);
        if (this.data.currentIndex < this.data.questionList.length - 1) {
            this.setData({
                currentIndex: this.data.currentIndex + 1,
                isAnimate: true
            })
        } else {
            let result = this.diffArray(this.data.questionRightAnswer, this.data.selectedAnswer);
            let { notSameArr, notSameArrIndex, someArr, someArrIndex } = result;
            this.setData({
                notSameArrIndex: someArrIndex
            })
            // 保存用户信息
            this.saveConfig().then(() => {
                wx.navigateTo({
                    url: `/pages/result/result?userInfo=${JSON.stringify(this.data.userInfo)}&selectedAnswer=${JSON.stringify(this.data.selectedAnswer)}&rightAnswer=${JSON.stringify(this.data.questionRightAnswer)}`,
                })
            });
        }
        this.backTop();
        setTimeout(() => {
            this.setData({
                isAnimate: false
            })
        }, 200)
    },
    // 上一题
    prevQuestion() {
        this.backTop();
        if (this.data.currentIndex > 0) {
            this.setData({
                currentIndex: this.data.currentIndex - 1
            })
        }
    },
    // 下一题
    nextQuestion() {
        this.backTop();
        if (this.data.currentIndex < this.data.questionList.length - 1) {
            this.setData({
                currentIndex: this.data.currentIndex + 1
            })
        }
    },
    // 返回顶部
    backTop() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300,
        });
    },
    // 存储用户信息
    saveConfig() {
        return new Promise((resolve, reject) => {
            app.cloud({
                name: 'user',
                data: {
                    $url: 'save_config',
                    hostId: this.data.hostId,
                    guestResult: this.data.notSameArrIndex,
                    guestAvatar: this.data.userInfo.avatarUrl,
                    guestNickName: this.data.userInfo.nickName,
                    score: this.data.notSameArrIndex.length * 10
                }
            }).then(res => {
                resolve(res);
            }).catch(err => {
                reject(err);
            })
        })
    },
    diffArray(arr1 = [], arr2 = []) {
        let someArr = [];
        let someArrIndex = [];
        let notSameArr = [];
        let notSameArrIndex = [];
        arr1.forEach((item, index) => {
            if (arr2.includes(item)) {
                someArr.push(item);
                someArrIndex.push(index);
            } else {
                notSameArr.push(item);
                notSameArrIndex.push(index);
            }
        })
        return {
            someArr,
            someArrIndex,
            notSameArr,
            notSameArrIndex
        }
    },
    // 订阅消息提醒
})
